package com.eqdd.yiqidian.ui.mine.pizhun;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.NoticeInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.NoticeResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.mine.renwu.TextActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

public class NoticeInfoActivity extends BaseActivity {
    private NoticeInfoActivityCustom dataBinding;
    private SlimAdapterEx<SlimEditBean> slimAdapterEx;
    private String noticeId;
    private ArrayList<SlimEditBean> datas;
    private int who;
    private int status;
    private NoticeResultBean items;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_notice_info);
        initTopTitleBar(View.VISIBLE, "公告详情");
    }

    @Override
    public void initData() {
        noticeId = getIntent().getStringExtra("noticeId");
        who = getIntent().getIntExtra(Config.WHO, 0);
        status = getIntent().getIntExtra(Config.STATUS, 0);
        noticeId = getIntent().getStringExtra("noticeId");
        initTitle();
        datas = new ArrayList<>();
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new SectionDividerItemDecoration(this));
        slimAdapterEx = SlimAdapterEx.create()
                .register(R.layout.list_item_28, new SlimInjector<SlimEditHintBean>() {
                    @Override
                    public void onInject(SlimEditHintBean data, IViewInjector injector) {
                        injector.text(R.id.tv_left_content, dealText(data.getTitle()))
                                .text(R.id.tv_right_hint_content, dealText(data.getContent()));
                    }
                }).attachTo(dataBinding.recyclerView);
        dataBinding.rbReject.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                dataBinding.rbAgree.setChecked(!isChecked);
            }
        });
        dataBinding.rbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                dataBinding.rbReject.setChecked(!isChecked);
            }
        });
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (position == 4) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", "公告内容");
                        bundle.putString("content", slimAdapterEx.getDataItem(position).getContent());
                        startActivity(TextActivity.class, bundle);
                    }
                });
    }

    private void initTitle() {
        if (who >= 1 && status == 0) {
            initTopRightText("提交", v -> {
                if (checkParam()) {
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.NOTICE_CHECK)
                            .params("noticeId", items.getId())
                            .params("userGuid", user.getGuid())
                            .params("message", dealText(dataBinding.etRemark.getText().toString()))
                            .params("type", dataBinding.rbAgree.isChecked() ? 1 : 2)
                            .execute(new DialogCallBack<HttpResult>(this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        setResult(Config.SUCCESS);
                                        finish();
                                    }
                                }
                            });
                } else {
                    ToastUtil.showLong("真残忍，能否给个理由先？");
                }
            });
        }
    }

    private boolean checkParam() {
        return !(dataBinding.rbReject.isChecked() && TextUtils.isEmpty(dataBinding.etRemark.getText()));
    }

    @Override
    public void setView() {

        OkGo.<HttpResult<NoticeResultBean>>post(HttpConfig.BASE_URL + HttpConfig.NOTICE_INFO)
                .params("noticeId", noticeId)
                .execute(new DialogCallBack<HttpResult<NoticeResultBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<NoticeResultBean>> response) {
                        HttpResult<NoticeResultBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            items = httpResult.getItems();
                            datas.add(new SlimEditHintBean("编码", items.getNoticeCode(), 0));
                            datas.add(new SlimEditHintBean("公告名字", items.getNoticeName(), 1));
                            datas.add(new SlimEditHintBean("类型", items.getObjectType() == 0 ? "所有人" : "部门", 2));
                            datas.add(new SlimEditHintBean("主题", items.getNoticeTheme(), 3));
                            datas.add(new SlimEditHintBean("内容", items.getNoticeContent(), 4));
                            datas.add(new SlimEditHintBean("创建者", items.getCreaterName(), 5));
                            datas.add(new SlimEditHintBean("责任", items.getDuty(), 6));
                            datas.add(new SlimEditHintBean("周期", items.getNoticeCycle(), 7));
                            if (!(who == 1 && status == 0)) {
                                datas.add(new SlimEditHintBean("审批人", items.getCheckerName(), 8));
                                datas.add(new SlimEditHintBean("审批时间", items.getCheckTime(), 9));
//                                datas.add(new SlimEditHintBean("审批状态", items.getCheckStatus() == null ? "" : items.getCheckStatus(), 10));
                                datas.add(new SlimEditHintBean("审批信息", items.getCheckMessage(), 10));
                            }
                            slimAdapterEx.updateData(datas);
                            if (who >= 1 && status == 0) {
                                dataBinding.llDownRoot.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                });
    }

    private String dealText(String text) {
        return TextUtils.isEmpty(text) ? " " : text;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
